//
//  ReimbursementTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/19.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ReimbursementTableViewController: PurchaseTableViewController {

    
    var details: [[Dictionary<String,String?>]] = []
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3 + details.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section {
        case 0:
            return 5
        case 1 + details.count:
            return 5
        case 2 + details.count:
            return documents.count
        default:
            return details[section-1].count
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 10
        } else if (section < details.count + 1) {
            return 44
        } else if (section == details.count + 1) {
            return 10
        } else {
            return 40
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section == 0 {
            return nil
        } else if section < details.count + 1 {
            let view = UIView()
            view.backgroundColor = UIColor.hex("eeeeee")
            let label = UILabel.init()
            label.text = "报销明细" + "(" + String(section) + ")"
            label.textColor = UIColor.hex("666666")
            label.font = UIFont.systemFont(ofSize: 14)
            label.width = label.text!.size(with: label.font).width
            label.height = label.text!.size(with: label.font).height
            label.left = 15
            label.top = 22 - label.height / 2
            view.addSubview(label)
            return view
        } else if section == details.count + 1 {
            return nil
        } else {
            let footer = UIView()
            
            footer.backgroundColor = UIColor.dftBack
            
            footer.backgroundColor = UIColor.white
            
            let titleLbl = UILabel()
            
            titleLbl.textColor = UIColor.textColor
            
            titleLbl.font = UIFont.boldSystemFont(ofSize: 18)
            
            if self.documents.count > 0 {
                titleLbl.text = "附件:"
            } else {
                titleLbl.text = "附件:无"
            }
            
            titleLbl.frame = CGRect(origin: CGPoint(x: 15, y: 0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
            
            titleLbl.top = 20 - titleLbl.contentSize.height / 2
            
            footer.addSubview(titleLbl)
            
            let line = UIView()
            line.backgroundColor = tableView.separatorColor
            footer.addSubview(line)
            line.snp.makeConstraints { (make) in
                make.left.equalTo(15)
                make.right.equalToSuperview()
                make.top.equalToSuperview()
                make.height.equalTo(1)
            }
            return footer
        }
        
        
        
    }
    
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.section == 0 {
            if let cel = cell as? ApprovalDetailCell {
                if indexPath.row < source.count {
                    cel.title = source[indexPath.row].keys.first
                    if let content = source[indexPath.row].first?.value {
                        cel.content = content
                    } else {
                        cel.content = ""
                    }
                }
            }
        } else if indexPath.section == 1 + details.count {
            if let cel = cell as? ApprovalDetailCell {
                if indexPath.row + 5 < source.count {
                    cel.title = source[indexPath.row + 5].keys.first
                    if let content = source[indexPath.row + 5].first?.value {
                        cel.content = content
                    } else {
                        cel.content = ""
                    }
                }
            }
        } else if indexPath.section == 2 + details.count {
            
        }else{
            if let cel = cell as? ApprovalDetailCell {
                if indexPath.row < details[indexPath.section - 1].count {
                    cel.title = details[indexPath.section - 1][indexPath.row].keys.first
                    if let content = details[indexPath.section - 1][indexPath.row].first?.value {
                        cel.content = content
                    } else {
                        cel.content = ""
                    }
                }
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
            
        case 2 + details.count:
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
                cell?.selectionStyle = .none
            }
            
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < documents.count {
                cell?.textLabel?.text = documents[indexPath.row].contents?["name"]?.rawString()
            }
            return cell!
            
        default:
            let cell =  tableView.dequeueReusableCell(withIdentifier: "ApprovalDetailCell")!
            cell.selectionStyle = .none
            return cell
        }
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            if source.count > indexPath.row {
                if let content = source[indexPath.row].first?.value {
                    
                    if content.count > 0{
                        
                        let size = content.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 40, height: 1000000))
                        
                        return 55 + size.height
                        
                    } else {
                        
                        return 55
                        
                    }
                } else {
                    return 55
                }
            } else {
                return 55
            }
        }else if (indexPath.section == 1 + details.count) {
            if source.count > 5 + indexPath.row {
                if let content = source[indexPath.row + 5].first?.value {
                    
                    if content.count > 0{
                        
                        let size = content.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 40, height: 1000000))
                        let str = "row=" + String(indexPath.row)
                        let hei = "height = " + String(describing: 55 + size.height)
                        if indexPath.row == 4 {
                           
                            print(str + hei)
                        }
                        return 55 + size.height
                        
                    }
                }
            } else {
                
                return 55
            }

        } else if (indexPath.section == 2 + details.count) {
            return 55
        } else {
            if let content = details[indexPath.section - 1][indexPath.row].first?.value {
                
                if content.count > 0{
                    
                    let size = content.size(with: UIFont.systemFont(ofSize: 15), maxSize: CGSize.init(width: tableView.width - 40, height: 1000000))
                    
                    return 55 + size.height
                    
                } else {
                    
                    return 55
                    
                }
            } else {
                return 55
            }
        }
        let str =  String(indexPath.section) + "row=" + String(indexPath.row)
        let hei = "height = " + String(55)
        print(str + hei)
        return 55
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == details.count + 2 {
            return super.tableView(tableView, didSelectRowAt: indexPath)
        }
    }

}
